﻿using System;
using System.Web;

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;

namespace Cognifide.PowerShell.Shell.Settings
{
    public class PsConsoleSettings
    {
        private static PsConsoleSettings instance;

        public static PsConsoleSettings Instance
        {
            get
            {
                if (instance == null || !instance.Loaded)
                {
                    instance = new PsConsoleSettings();
                    instance.Load();
                }
                return instance;
            }
        }

        protected bool Loaded { get; private set; }

        private const string PLUGIN_SETTINGS_ITEM_PATH = "/sitecore/content/Applications/PowerShell Console/All Users";

        public string PreScript { get; set; }
        public string LastScript { get; set; }
        public bool SaveLastScript { get; set; }
        public bool UseTypeInfo { get; set; }
        public int HostWidth { get; set; }
        public ConsoleColor ForegroundColor { get; set; }
        public ConsoleColor BackgroundColor { get; set; }


        private Item GetSettingsDTO()
        {
            Database currentDB = Factory.GetDatabase("core");
            Item currentItem = currentDB.GetItem(PLUGIN_SETTINGS_ITEM_PATH);
            return currentItem;
        }

        public void Save()
        {
            Item configuration = GetSettingsDTO();
            configuration.Edit(
                p =>
                    {
                        configuration["PreScript"] = HttpUtility.HtmlEncode(PreScript);
                        configuration["LastScript"] = HttpUtility.HtmlEncode(LastScript);
                        ((CheckboxField) configuration.Fields["SaveLastScript"]).Checked = SaveLastScript;
                        ((CheckboxField)configuration.Fields["UseTypeInfo"]).Checked = UseTypeInfo;
                        configuration["HostWidth"] = HostWidth.ToString();
                        configuration["ForegroundColor"] = ForegroundColor.ToString();
                        configuration["BackgroundColor"] = BackgroundColor.ToString();
                    });
        }

        internal void Load()
        {
            Item configuration = GetSettingsDTO();

            if (configuration != null)
            {
                PreScript = HttpUtility.HtmlDecode(configuration["PreScript"]);
                LastScript = HttpUtility.HtmlDecode(configuration["LastScript"]);
                SaveLastScript = ((CheckboxField)configuration.Fields["SaveLastScript"]).Checked;
                UseTypeInfo = ((CheckboxField)configuration.Fields["UseTypeInfo"]).Checked;
                int hostWidth;
                HostWidth = Int32.TryParse(configuration["HostWidth"], out hostWidth) ? hostWidth : 150;
                try
                {
                    ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), configuration["ForegroundColor"]);
                }
                catch // disregard parsing error & set default
                {                    
                    ForegroundColor = ConsoleColor.White;
                }

                try
                {
                    BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), configuration["BackgroundColor"]);
                }
                catch // disregard parsing error & set default
                {
                    BackgroundColor = ConsoleColor.DarkBlue;
                }
                Loaded = true;
            }
        }
    }

}